<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <div class="area-header">
          <span class="area-header-title">{{ $t('财务状况') }}</span>
        </div>
        <el-col :span="12">
          <el-form-item :label="$t('会计年度')" prop="year">
            <DatePicker
              v-model="form.year"
              type="year"
              value-format="yyyy"
              :placeholder="$t('会计年度')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总资产') + $t('金额单位')" prop="totalAsset">
            <Money v-model="form.totalAsset" unit="w" :placeholder="$t('总资产')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('净资产') + $t('金额单位')" prop="netAsset">
            <Money v-model="form.netAsset" unit="w" :placeholder="$t('净资产')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总负债') + $t('金额单位')" prop="totalLiabilities">
            <Money v-model="form.totalLiabilities" unit="w" :placeholder="$t('总负债')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总收入') + $t('金额单位')" prop="totalIncome">
            <Money v-model="form.totalIncome" unit="w" :placeholder="$t('总收入')" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row style="margin-top: 20px">
        <div class="area-header">
          <span class="area-header-title">{{ $t('营业情况') }}</span>
        </div>
        <el-col :span="12">
          <el-form-item :label="$t('管理费') + $t('金额单位')" prop="manageFee">
            <Money v-model="form.manageFee" unit="w" :placeholder="$t('管理费')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('业绩报酬') + $t('金额单位')" prop="performance">
            <Money v-model="form.performance" unit="w" :placeholder="$t('业绩报酬')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('其他收入') + $t('金额单位')" prop="otherIncome">
            <Money v-model="form.otherIncome" unit="w" :placeholder="$t('其他收入')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('营业收入合计') + $t('金额单位')" prop="totalOperateIncome">
            <Money v-model="form.totalOperateIncome" unit="w" :placeholder="$t('营业收入合计')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('职工薪酬') + $t('金额单位')" prop="employeeSalary">
            <Money v-model="form.employeeSalary" unit="w" :placeholder="$t('职工薪酬')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('外包服务费用支出') + $t('金额单位')" prop="serviceExpend">
            <Money v-model="form.serviceExpend" unit="w" :placeholder="$t('外包服务费用支出')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('其他支出') + $t('金额单位')" prop="otherExpend">
            <Money v-model="form.otherExpend" unit="w" :placeholder="$t('其他支出')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('营业支出合计') + $t('金额单位')" prop="totalOperateExpend">
            <Money v-model="form.totalOperateExpend" unit="w" :placeholder="$t('营业支出合计')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('营业利润') + $t('金额单位')" prop="operatingProfits">
            <Money v-model="form.operatingProfits" unit="w" :placeholder="$t('营业利润')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('净利润') + $t('金额单位')" prop="netProfits">
            <Money v-model="form.netProfits" unit="w" :placeholder="$t('净利润')" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row style="margin-top: 20px">
        <div class="area-header">
          <span class="area-header-title">{{ $t('上一年审计报告信息') }}</span>
        </div>
        <el-col :span="12">
          <el-form-item :label="$t('是否有审计报告')" prop="whetherAuditReport">
            <dict-select
              v-model="form.whetherAuditReport"
              dictionary="sys_yes_no"
              :placeholder="$t('是否有审计报告')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('审计报告中会计事务所出具的是否是标准意见')" prop="auditOpinion">
            <el-input v-model="form.auditOpinion" :placeholder="$t('审计报告中会计事务所出具的是否是标准意见')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('会计事务所名称')" prop="accountFirmName">
            <el-input v-model="form.accountFirmName" :placeholder="$t('会计事务所名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('事务所联系人名称')" prop="firmContactName">
            <el-input v-model="form.firmContactName" :placeholder="$t('事务所联系人名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('办公地址')" prop="officeAddress">
            <el-input v-model="form.officeAddress" :placeholder="$t('办公地址')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="area-header" style="margin-top: 10px">
        <span class="area-header-title">{{ $t('相关方信息') }}</span>
      </div>
      <el-table tooltip-effect="darkTable" :data="form.partyList" border class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('机构名称')" align="center" prop="orgName">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('机构名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'partyList.' + scope.$index + '.orgName'" :rules="partyRules.orgName">
              <el-input v-model="scope.row.orgName" :placeholder="$t('机构名称')" show-word-limit :maxlength="100" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('经营范围')" align="center" prop="businessScope">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('经营范围') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'partyList.' + scope.$index + '.businessScope'" :rules="partyRules.businessScope">
              <el-input v-model="scope.row.businessScope" :placeholder="$t('机构名称')" show-word-limit :maxlength="100" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" prop="remark">
          <template slot-scope="scope">
            <el-form-item :prop="'partyList.' + scope.$index + '.remark'">
              <el-input v-model="scope.row.remark" :placeholder="$t('备注')" show-word-limit :maxlength="1000" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="delParty(scope.$index, scope.row)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addParty">{{ $t("添加") }}</el-button>
      </div>
      <!--  附件信息    -->
      <FileTable :list.sync="form.fileList" data-type="gp_financial" :business-id="this.$route.query.gpId" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="gp:financial" />
    </div>
  </div>
</template>

<script>
import { saveGpFinancialInfo, getGpFinancialInfo } from '@/api/fund/gp/financial/gpFinancialInfo'
import { delGpFinancialParty } from '@/api/fund/gp/financial/gpFinancialParty'

export default {
  data() {
    return {
      otherBtnHide: false,
      // 表单参数
      form: {
        fileList: [],
        partyList: []
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        year: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        accountFirmName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        totalAsset: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      partyRules: {
        orgName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        businessScope: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        getGpFinancialInfo(data.id).then(response => {
          this.form = response.data
        })
      } else {
        this.reset()
      }
    },
    // 添加相关方信息
    addParty() {
      const row = {
        id: undefined,
        gpId: this.$route.query.gpId,
        financialId: this.form.id,
        orgName: '',
        businessScope: '',
        remark: ''
      }
      if (!(this.form.partyList instanceof Array)) {
        this.$set(this.form, 'partyList', [])
      }
      this.$nextTick(() => {
        this.form.partyList.push(row)
      })
    },
    // 删除用印明细
    delParty(index, row) {
      const id = row.id
      if (id !== undefined && id !== '' && id != null) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
        .then(function() {
          return delGpFinancialParty(id)
        }).then(() => {
          this.form.partyList.splice(index, 1)
          return this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
      } else {
        this.form.partyList.splice(index, 1)
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gpId: this.$route.query.gpId,
        year: undefined,
        totalAsset: undefined,
        netAsset: undefined,
        totalLiabilities: undefined,
        totalIncome: undefined,
        manageFee: undefined,
        performance: undefined,
        otherIncome: undefined,
        totalOperateIncome: undefined,
        employeeSalary: undefined,
        serviceExpend: undefined,
        otherExpend: undefined,
        totalOperateExpend: undefined,
        operatingProfits: undefined,
        netProfits: undefined,
        whetherAuditReport: undefined,
        auditOpinion: undefined,
        accountFirmName: undefined,
        firmContactName: undefined,
        officeAddress: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined,
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveGpFinancialInfo(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
